package org.n.shop.controller;

import lombok.AllArgsConstructor;
import org.n.shop.annotation.RequiresLogin;
import org.n.shop.domain.dto.LoginDto;
import org.n.shop.domain.entity.User;
import org.n.shop.result.RestResult;
import org.n.shop.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.TimeUnit;

import static org.n.shop.config.RedisConfig.PREFIX_ORDER;

@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {
    private UserService userService;
    private RedisTemplate redisTemplate;

    @RequestMapping("/login")
    public RestResult login(@RequestBody User user) {
        return userService.login(user);
    }

    @RequiresLogin
    @RequestMapping("/userinfo")
    public RestResult userinfo(@RequestHeader("authorization") String authorization) {
        return userService.userinfo(authorization);
    }

    @RequiresLogin
    @RequestMapping("/update")
    public RestResult update(@RequestHeader("authorization") String authorization, @RequestBody User user) {
        return userService.update(authorization, user);
    }

    @RequestMapping("/test")
    public RestResult test() {
        redisTemplate.opsForValue().set(PREFIX_ORDER + 1, 1, 1, TimeUnit.SECONDS);
        return null;
    }

    @RequiresLogin
    @RequestMapping("/upload/avatar")
    public RestResult uploadAvatar(@RequestHeader("authorization") String authorization,
                                   @RequestParam("avatar") MultipartFile multipartFile) {
        return userService.uploadAvatar(authorization, multipartFile);
    }
}
